﻿using API.Common;
using API.Constant;
using API.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace API.Controllers.Api
{
    public class LessonController : ApiController
    {
        private DataLinqDataContext db = new DataLinqDataContext();
        private Dictionary<string, object> ResponseStatus = new Dictionary<string, object>();
        private Dictionary<string, object> returnData = new Dictionary<string, object>();
        private ReturnStatusConstant returnStatus;

        /// <summary>
        /// Get Lesson
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetLesson")]
        public HttpResponseMessage GetLesson(int id)
        {
            try
            {
                var checkHeader = APICommon.CheckEtoken(Request.Headers);
                if (checkHeader)
                {
                    var getLesson = (from m in db.tbl_LessonInfos
                                     where m.IdType == id
                                     select new Lesson
                                       {
                                           Id = m.Id.ToString(),
                                           IdType = m.IdType.ToString(),
                                           Logo = m.Logo,
                                           Title = m.Title
                                       }).ToList();

                    returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeNone, Status.Success, MessageResponse.MessageNone, APICommon.Etoken);
                    returnData.Add("status", returnStatus);
                    returnData.Add("returnLesson", getLesson);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
                else
                {
                    returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeInvalidUserName, Status.Faiure, MessageResponse.MessageInvalidEtoken, string.Empty);
                    returnData.Add("status", returnStatus);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            //return Request.CreateResponse(HttpStatusCode.OK);
        }

        /// <summary>
        /// Get Lesson
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [ActionName("GetLessonDetail")]
        public HttpResponseMessage GetLessonDetail(int id)
        {
            try
            {
                var checkHeader = APICommon.CheckEtoken(Request.Headers);
                if (checkHeader)
                {
                    var getLesson = (from m in db.tbl_LessonInfos
                                     where m.Id == id
                                     select new Lesson
                                     {
                                         Id = m.Id.ToString(),
                                         IdType = m.IdType.ToString(),
                                         Content = m.Content,
                                         Logo = m.Logo,
                                         Title = m.Title
                                     }).ToList();

                    returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeNone, Status.Success, MessageResponse.MessageNone, APICommon.Etoken);
                    returnData.Add("status", returnStatus);
                    returnData.Add("returnLesson", getLesson);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
                else
                {
                    returnStatus = new ReturnStatusConstant(ErrorCode.ErrorCodeInvalidUserName, Status.Faiure, MessageResponse.MessageInvalidEtoken, string.Empty);
                    returnData.Add("status",returnStatus);
                    return Request.CreateResponse(HttpStatusCode.Created, returnData);
                }
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }
            //return Request.CreateResponse(HttpStatusCode.OK);
        }
    }
}
